#    LOL Man pages
#    Copyright (C) 2010  kishore@logonlinux.com
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
import collections
import cgi
import os
import datetime
import re
import urllib

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.api import users
from google.appengine.ext import db
from base import *
from model import *
from utils import *
from forms import *

# main handler to show the index page
class MainHandler(BaseRequestHandler):
    def get(self):
        self.generate('index.html')

# Right now Man page supports only commands
class ManPage(BaseRequestHandler):
    def get(self,type,page_name):
        
        #values for the page input    
        values = {} 
        mode = self.request.get("mode",default_value="view")
        
        if not page_name:
            self.redirect('/')
        else:
            if type == 'command':            
                page_name = urllib.unquote(page_name)
                self.generate('page.html',{"pageName":page_name})
            else:
                self.generate('error.html',{"error":"Resource not found"})
                
    # post method
    @requiresAdmin
    def post(self):            
            self.generate('error.html',{'error':"Resource not found"}) 

# By default the search keys are considered as commands 
# additionally the keys can be c functions, etc
class DefaultPage(BaseRequestHandler):
    def get(self,page_name):
        self.redirect('/command/'+page_name)
        
class MainPage(BaseRequestHandler):
    def get(self):
        self.generate('forms.html',{"page":ManPageForm()})

# 
def main():
    application = webapp.WSGIApplication([('/', MainHandler),
                                            ('/([^/]+)?', DefaultPage),
                                            ('/([^/]+)?/([^/]+)?', ManPage),
                                            ('/(.*)',NotFound)
                                            ],
                                         debug=True)
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()
